<?php

namespace backend\models;

use Yii;
use common\models\UserInfo;
use backend\models\AdminInfo;
use yii\helpers\ArrayHelper;
use backend\models\GrpCity;
use yii\db\ActiveRecord;
use yii\data\ActiveDataProvider;

class OperatorRelation extends \yii\db\ActiveRecord
{
    
    public $city1;
    public $city2;
    public $city3;
    public $city1List;
    public $city_list_old;
    public $tel;
    public $username;
    /**
     * 添加页面 获取所有省份 去重
     * @return string|\yii\db\ActiveRecord
     */
    public function getAllProvince(){
        $t=ActiveRecord::findBySql('SELECT id,name FROM  grp_city  WHERE (id%10000)=0 ')->asArray()->all();
        for($j=0;$j<count($t);$j++){
            //获取所属于他的城市
            $provinceId=strval($t[$j]['id']);
            $specilProvince=array('710000','810000','820000');//香港 澳门  台湾 没有下级，特殊处理
            if(in_array($provinceId, $specilProvince)){
                $cityInfo['id']=$t[$j]['id'];
                $cityInfo['name']=$t[$j]['name'];
                $city_list[]=$t[$j];
            }
            $specilProvince=array('110000','120000','310000','500000');//直辖市特殊处理
            $proprev=substr($provinceId, 0,2);//省份开头两位数字
            if(in_array($provinceId, $specilProvince)){
                $sql="select id,name from grp_city where id like  '$proprev%' and id !=$provinceId";
            }else{
                $sql="select id,name from grp_city where id LIKE '$proprev%00' and id !=$provinceId ";
            } 
            $allCity=ActiveRecord::findBySql($sql)->asArray()->all();
            $chooseCount=0;//判断被选中的城市总数是否和所有城市总数相等，如果相等，当前省份不可以被选择
            for($i=0;$i<count($allCity);$i++){
                $cityId=$allCity[$i]['id'];
                $sql="SELECT uid from prf_operator_relation   WHERE  city_list  LIKE '%$cityId%' and useing=1 ";
                $opreatorRelation=ActiveRecord::findBySql($sql)->asArray()->One();
                if(count($opreatorRelation)>0){//已经被选择为运营商管辖区域，不可下拉再次选择
                    $chooseCount++;
                }
            }
            if($chooseCount<count($allCity)){
                $cityInfo['id']=$t[$j]['id'];
                $cityInfo['name']=$t[$j]['name'];
                $city_list[]=$t[$j];
            }
        }
        $city1List[0]='请选择';
        foreach($city_list as $value){
            $city1List[$value['id']]=$value['name'];
        }
        return  $city1List;
    }
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'prf_operator_relation';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {    	
        return [
            [['uid','parent'], 'number'],
            [['city1'],'integer','min'=>110000,'tooSmall'=>'请选择省份！'],
        	[['uid','city1'],'required'],
            [['id'],'integer'],
            // [['uid'],'unique','message'=>'运营商已存在'],  
          // [['uid'], 'exist','targetClass' => '\common\models\UserInfo', 'targetAttribute'=>['uid' => 'id','uid'=>'tel'],'message' => '用户不存在!'],
           // [['parent'], 'exist','targetClass' => '\common\models\UserInfo', 'targetAttribute'=>['parent' => 'id','parent'=>'tel'],'message' => '运营商不存在!'],
            [['city_id','city_list','memo','useing','username'],'safe']
        ];
    }
  
    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'uid' => 'uid或手机号',
            'city1' => '省份',
            'city2' => '城市',
            'city3' => '区县',
            'city_id' => Yii::t('app','所属城市'),
            'city_list' => Yii::t('app','管理地区'),
            'parent' => Yii::t('app','上级(uid或手机号)'),
            'memo' => Yii::t('app','说明'),
            'tel'=>Yii::t('app','用户名')
        ];
    }
   /**
    * 根据用户填入的uid或手机号查询uid
    * @param   $uid
    */
    public function getuid($uid,$type){
        $sql="select id from user_info where id=:id and user_type=5";
        $param=array(":id"=>$uid);
        $userInfo=UserInfo::findBySql($sql,$param)->asArray()->One();
        if(count($userInfo)>0){
                return $userInfo['id'];
        }else{
            $sql="select id from user_info where tel=:id and user_type=5";
            $userInfo=UserInfo::findBySql($sql,$param)->asArray()->One();
            if(count($userInfo)>0){
                return $userInfo['id'];
            }else{
                return 0;
            }
            
        }
    }
    /**
     * 获取具体的city_id
     * @param   $operatorRelation
     * @return  
     */
    public function getCityIdByPost($operatorRelation){
        if(key_exists("city3", $operatorRelation)&&intval($operatorRelation['city3'])>0){
            $city_id=$operatorRelation['city3'];
        }elseif(key_exists("city2", $operatorRelation)&&intval($operatorRelation['city2'])>0){
            $city_id=$operatorRelation['city2'];
        }else{
            $city_id=$operatorRelation['city1'];
        }
        return $city_id;
    }
    /**
     * 根据用户选择的城市 来获取所属地区  注：当用户没有选择所属地区的时候
     */
    public function getcity_listByPost($operatorRelation){
        //当前到区，查询为当前城市的所有区县
        if(key_exists("city2", $operatorRelation)&&intval($operatorRelation['city2'])>0){
            $city2=intval($operatorRelation['city2']);
            $city3List=GrpCity::find()->where(['like','id',intval($city2/100).'%',false])->andWhere(['<>','id',$city2])->asArray()->all();
            if(count($city3List)>0){
                $city_list='';
                for($i=0;$i<count($city3List);$i++){
                    if($i==0){
                        $city_list.=$city3List[$i]['id'];
                    }else{
                        $city_list.=",".$city3List[$i]['id'];
                    }
                }
            }else{
                $city_list='';
            }
        }else{
            $provinceId=$operatorRelation['city1'];
            $proprev=substr($provinceId,0,2);//省份开头两位数字
            //有可能是直辖市
            $specilProvince=array('110000','120000','310000','500000'); 
            if(in_array($provinceId, $specilProvince)){
                $city2List=GrpCity::find()->where(['like','id',$proprev.'%',false])->andWhere(['<>','id',$provinceId])->asArray()->all();
            }else{
                $city2List=GrpCity::find()->where(['like','id',$proprev.'%00',false])->andWhere(['<>','id',$provinceId])->asArray()->all();
            }
            if(count($city2List)>0){
                $city_list='';
                for($i=0;$i<count($city2List);$i++){
                    if($i==0){
                        $city_list.=$city2List[$i]['id'];
                    }else{
                        $city_list.=",".$city2List[$i]['id'];
                    }
                }
            }else{
                $city_list='';
            }
            
        }
        return $city_list;
        
    }
    /**
     * 省份 反填充
     */
    public function getProvince($cityId){
        $cityInfo=GrpCity::findBySql('SELECT id,name  FROM `grp_city` WHERE id=:id',array(":id"=>$cityId))->asArray()->one();
        $city1List=[$cityInfo['id'] => $cityInfo['name']];
        $t=GrpCity::findBySql('SELECT * FROM `grp_city` WHERE (id%10000)=0 and id!=:id',array(":id"=>$cityInfo['id']))->asArray()->all();
        //查询每个省份的城市有没有被全部选中 
        for($j=0;$j<count($t);$j++){
            //获取所属于他的城市
            $provinceId=strval($t[$j]['id']);
            $specilProvince=array('710000','810000','820000');//香港 澳门  台湾 没有下级，特殊处理
            if(in_array($provinceId, $specilProvince)){
                $cityInfo['id']=$t[$j]['id'];
                $cityInfo['name']=$t[$j]['name'];
                $city_list[]=$t[$j];
            }
            $specilProvince=array('110000','120000','310000','500000');//直辖市特殊处理
            $proprev=substr($provinceId, 0,2);//省份开头两位数字
            if(in_array($provinceId, $specilProvince)){
                $sql="select id,name from grp_city where id like  '$proprev%' and id !=$provinceId";
            }else{
                $sql="select id,name from grp_city where id LIKE '$proprev%00' and id !=$provinceId ";
            }
            $allCity=ActiveRecord::findBySql($sql)->asArray()->all();
            $chooseCount=0;//判断被选中的城市总数是否和所有城市总数相等，如果相等，当前省份不可以被选择
            for($i=0;$i<count($allCity);$i++){
                $cityId=$allCity[$i]['id'];
                $sql="SELECT uid from prf_operator_relation   WHERE  city_list  LIKE '%$cityId%' and useing=1";
                $opreatorRelation=ActiveRecord::findBySql($sql)->asArray()->One();
                if(count($opreatorRelation)>0){//已经被选择为运营商管辖区域，不可下拉再次选择
                    $chooseCount++;
                }
            }
            if($chooseCount<count($allCity)){
                $cityInfo['id']=$t[$j]['id'];
                $cityInfo['name']=$t[$j]['name'];
                $city_list[]=$t[$j];
            }
        }
        foreach($city_list as $value){
            $city1List[$value['id']]=$value['name'];
        }
        return $city1List;
    }
    /**
     * 根据city_id获取city_name
     */
    public function getCityName($city_id){
        $str=substr($city_id,2);
        if($str=="0000"){
            //省份
            $Province=GrpCity::findOne($city_id);
            $city_name=$Province['name'];
            $cityid_name=$city_name;
        }else{
            $str=substr($city_id, 4);
            $firstStr=substr($city_id, 0,2);
            $provinceId=intval($firstStr."0000");
            $Province=GrpCity::findOne($provinceId);
            $province_name=$Province['name'];
            if($str=="00"){
                //计算城市
                $city=GrpCity::findOne($city_id);
                $city_name=$city['name'];
                $cityid_name=$province_name.",".$city_name;
            }else{
                //区县
                $firstStr=substr($city_id, 0,4);
                $cId=intval($firstStr."00");
                $city=GrpCity::findBySql('SELECT id,name  FROM `grp_city` WHERE id=:id',array(":id"=>$cId))->asArray()->One();
                $city_name=$city['name'];
                $country=GrpCity::findBySql('SELECT id,name  FROM `grp_city` WHERE id=:id',array(":id"=>$city_id))->asArray()->One();
                $country_name=$country['name'];
                if($city_name!=""){
                    $cityid_name=$province_name.",".$city_name.",".$country_name;
                }else{
                    $cityid_name=$province_name.",".$country_name;
                }
            }
        }
        return $cityid_name;
    }
    /**
     * 获取城市名称
     * @param   $city_list_str
     * @return string
     */
    public function getCitylistStr($city_list_str){
        $city_list=explode(",",$city_list_str);
        $city_str='';
        for($j=0;$j<count($city_list);$j++){
            $city_name=GrpCity::findOne($city_list[$j]);
            if($j==count($city_list)-1){
                $city_str.=$city_name['name'];
            }else{
                $city_str.=$city_name['name']." , ";
            }
            
        }
        return $city_str;
    }
    /**
     * 根据省份获取城市    
     */
    public function getCitylistByProvince($provinceId){
        //如果是直辖市，直接查询下级的区县
        $specilProvince=array('110000','120000','310000','500000');//直辖市
        if(in_array($provinceId, $specilProvince)){
            $firstStr=substr($provinceId, 0,2);
            $value=GrpCity::find()->where(['like','id',$firstStr.'%',false])->andWhere(['<>','id',$provinceId])->asArray()->all();
            
        }else{
            $firstStr=substr($provinceId, 0,2);
            $value=GrpCity::find()->where(['like','id',$firstStr.'%00',false])->andWhere(['<>','id',$provinceId])->asArray()->all();
        }
         $count=count($value);
        //删除已被选中的城市
        for($i=0;$i<$count;$i++){
            $cityid=$value[$i]['id'];
            $sql="select uid from prf_operator_relation where city_list like '%$cityid%' and useing=1";
            $operator=OperatorRelation::findBySql($sql)->asArray()->all();
            if(count($operator)>0){//如果运营商表存在这个城市，则去除
                unset($value[$i]);
            }else{//遍历该城市的下级
                //$countryList= GrpCity::area($cityid);
                $countryList= GrpCity::find()->where(['like','id',intval($cityid/100).'%',false])->andWhere(['<>','id',$cityid])->asArray()->all();;
                $chooseCount=0;
                if(count($countryList)>0){
                    for($j=0;$j<count($countryList);$j++){
                        $cityId=$countryList[$j]['id'];
                        $sql="SELECT uid from prf_operator_relation   WHERE  city_list  LIKE '%$cityId%'  and useing=1 ";
                        $opreatorRelation=ActiveRecord::findBySql($sql)->asArray()->One();
                        if(count($opreatorRelation)>0){//已经被选择为运营商管辖区域，不可下拉再次选择
                            $chooseCount++;
                        }
                    }
                    if($chooseCount>=count($countryList)){
                        unset($value[$i]);
                    }
                }
            }
        }
        return $value;
    }
    /**
     * 根据省份获取管辖区域城市
     * @param   $provinceId
     * @return  
     */
    public function getGxCitylistByProvince($provinceId){
        $firstStr=substr($provinceId, 0,2);
        $value=GrpCity::find()->where(['like','id',$firstStr.'%00',false])->andWhere(['<>','id',$provinceId])->asArray()->all();
        $count=count($value);
        //删除已被选中的城市
        for($i=0;$i<$count;$i++){
            $cityid=$value[$i]['id'];
            $sql="select uid from prf_operator_relation where city_list like '%$cityid%' and useing=1";
            $operator=OperatorRelation::findBySql($sql)->asArray()->all();
            if(count($operator)>0){//如果运营商表存在这个城市，则去除
                unset($value[$i]);
            }else{//遍历该城市的下级
                //$countryList= GrpCity::area($cityid);
                $countryList= GrpCity::find()->where(['like','id',intval($cityid/100).'%',false])->andWhere(['<>','id',$cityid])->asArray()->all();;
                $chooseCount=0;
                if(count($countryList)>0){
                    for($j=0;$j<count($countryList);$j++){
                        $cityId=$countryList[$j]['id'];
                        $sql="SELECT uid from prf_operator_relation   WHERE  city_list  LIKE '%$cityId%'  and useing=1 ";
                        $opreatorRelation=ActiveRecord::findBySql($sql)->asArray()->One();
                        if(count($opreatorRelation)>0){//只要有一个区县被选中，当前城市则不可以被选择
                            unset($value[$i]);
                        }
                    }
                }
            }
        }
        return $value;
    }
    
    /**
     * 根据城市id获取下面的区县列表
     */
    public function getCountrylistByCity($cityId){
        $value = GrpCity::area($cityId);
        $count=count($value);
        //删除已被选中的城市
        for($i=0;$i<$count;$i++){
            $cityid=$value[$i]['id'];
            $sql="select uid from prf_operator_relation where city_list like '%$cityid%' and useing=1";
            $operator=OperatorRelation::findBySql($sql)->asArray()->all();
            if(count($operator)>0){//如果运营商表存在这个城市，则去除
                unset($value[$i]);
            }else{//遍历该城市的下级
               // $countryList= GrpCity::area($cityid);
                $countryList= GrpCity::find()->where(['like','id',intval($cityid/100).'%',false])->andWhere(['<>','id',$cityid])->asArray()->all();;
                $chooseCount=0;
                if(count($countryList)>0){
                    for($j=0;$j<count($countryList);$j++){
                        $cityId=$countryList[$j]['id'];
                        $sql="SELECT uid from prf_operator_relation   WHERE  city_list  LIKE '%$cityId%' and useing=1";
                        $opreatorRelation=ActiveRecord::findBySql($sql)->asArray()->One();
                        if(count($opreatorRelation)>0){//已经被选择为运营商管辖区域，不可下拉再次选择
                            $chooseCount++;
                        }
                    }
                    if($chooseCount>=count($countryList)){
                        unset($value[$i]);
                    }
                }
            }
        }
        return $value;
    }
    
    /**
     * 点击提交   当管辖区域为空，根据省份城市区县 去获取 当前省份 或者城市 或者区县 有没有被选择一个，如果被选择了，则不可以为空
     */
    public function checkChoose($provinceId,$cityId,$countryId){
        $ifExits=2;
        if(intval($countryId)>0){
            //如果选择了区，查看当前城市的所有区县
            $prevStr=substr($countryId, 0,4);
            $sql="select id,name from grp_city where id like '$prevStr%' ";//同一个市下的所有区县
        }elseif(intval($cityId)>0){
            //城市 或者直辖市    查看当前城市的所有区县
            $lastStr=substr($cityId, 4,2);
            if($lastStr=="00"){//普通城市
                $prevStr=substr($cityId, 0,4);
                $sql="select id,name from grp_city where id like '$prevStr%'";
            }else{//直辖市  获取当前直辖市
                $proprev=substr($cityId,0,2);
                $sql="select id,name from grp_city where id like '$proprev%'";
            }
        }elseif(intval($provinceId)>0){
            //省份  查询当前省的所有城市
            $proprev=substr($provinceId,0,2);
            $specilProvince=array('110000','120000','310000','500000');//直辖市
            if(in_array($provinceId, $specilProvince)){
                $sql="select id,name from grp_city where id like '$proprev%'";
            }else{
                $sql="select id,name from grp_city where id like '$proprev%00'";
            }
        }
        $cityList=GrpCity::findBySql($sql)->asArray()->all();
        for($i=0;$i<count($cityList);$i++){
            $cityId=$cityList[$i]['id'];
            $sql="select uid from prf_operator_relation where city_list like $cityId and useing=1 ";
            $operator=OperatorRelation::findBySql($sql)->asArray()->all();
            if(count($operator)>0){
                $ifExits=1;
                break;
            }
            $sql="select uid from prf_operator_relation where city_id like $cityId and useing=1";
            $operator=OperatorRelation::findBySql($sql)->asArray()->all();
            if(count($operator)>0){
                $ifExits=1;
                break;
            }
            
        }
        return $ifExits;
    }
    /**
     * 修改页面，根据选择的城市或者区县，获取所有管辖区域
     */
    public function getCitylistOnUpdateByCityid($cityid,$id,$citylistArray){
        $citylist=array();
        $zhixiashi=false;
        if(substr($cityid, 4,3)=="00"){
            $lastStr=substr($cityid, 2,4);
            //可能是直辖市，也可能是城市,也可能是省份
            $specilProvince=array('110000','120000','310000','500000');//直辖市特殊处理
            if(in_array($cityid, $specilProvince)){
                $zhixiashi=true;
                $cityId=intval($cityid/10000)."0000";
                $like_id=intval($cityid/10000)."%";
            }else{
                if($lastStr=="0000"){//省份
                    $cityId=intval($cityid/10000)."0000";
                    $like_id=intval($cityid/10000)."%00";
                }else{
                    $cityId=$cityid;
                    $like_id=substr($cityid,0,4)."%";
                }
            }
        }else{
            $cityId=intval($cityid/10000)."0000";
            $like_id=intval($cityid/10000)."%00";
        }
       $sql="select id,name from grp_city where id like '$like_id' and id !=$cityId  order by id asc";
        $value=GrpCity::findBySql($sql)->asArray()->all();
        //判断当前区域有没有被选择，如果被选择，清除
        $count=count($value);
        for($i=0;$i<$count;$i++){
            $cityId=$value[$i]['id'];
             $sql="select uid from prf_operator_relation where useing=1 and  city_list like '%$cityId%' and id!=$id";
            $operator=OperatorRelation::findBySql($sql)->asArray()->all();
            if(count($operator)>0){
                unset($value[$i]);
            }
        }
        
        foreach($citylistArray as $clist){
            $sql="select id,name from grp_city where id =$clist";
            $CityInfo=GrpCity::findBySql($sql)->asArray()->One();
            $cityid=$CityInfo['id'];
            $name=$CityInfo['name'];
            $cInfo=array('id'=>$cityid,"name"=>$name);
            //  $citylist[]=$CInfo;
            // $citylist[][$CityInfo['id']]=$CityInfo['name'];
            $value[]=$cInfo;
        }
        foreach($value as $v){
            $citylist[$v['id']]=$v['name'];
        }
        return $citylist;
    }
    /**
     * 修改页面，反填充省份  
     */
    public function getFormartAllProvinceOnUpdate($value){
        $city2[0]="请选择";
        foreach($value as $v){
            $city2[$v['id']]=$v['name'];
        }
        return $city2;
    }
    /**
     * 获取当前的登录的运营商的城市id    可能cityid为省份 直辖市 城市 区县  ，统一返回城市id/直辖市区县id
     * 查询他的下级的城市列表，也要给他加上
     */
    public function getCityIdById($uid){
        if(intval($uid)>0){
            $sql="select * from prf_operator_relation where (uid=:uid and useing=1) or (parent=:uid and useing=1)";
            $param=array(":uid"=>$uid);
            $operatorRealtionList=OperatorRelation::findBysql($sql,$param)->asArray()->all();
            $cityList_item=array();//城市列表
            for($i=0;$i<count($operatorRealtionList);$i++){
                $city_id=intval($operatorRealtionList[$i]['city_id']);
                $specilProvince=array('110000','120000','310000','500000'); 
                //city_id为省， 那么从city_list 获取cityid 
                if($city_id%10000==0){
                    if(in_array($city_id, $specilProvince)){
                        //直辖市
                        $cityList_item[]=$city_id;
                    }else{
                        $citylistStr=$operatorRealtionList[$i]["city_list"];
                        //省份--根据city_list 获取城市 -- 可能多个城市
                         $citylist_array=explode(",",$citylistStr);
                         for($j=0;$j<count($citylist_array);$j++){
                             $cityList_item[]=$citylist_array[$j];
                         }
                    }
                //city_id为市    
                }else{
                    if($city_id%100==0){//城市
                        $cityList_item[]=$city_id;
                    } 
                }
            }
            $cityList_item=array_unique($cityList_item);
            $cityList_Str=implode(",",$cityList_item);
            return $cityList_Str;
        }else{
            return "";
        }
    }
    /**获取当前的登录的运营商的城市名称    包括下级运营商   可能cityid为省份 直辖市 城市 区县  ，统一返回城市id/直辖市区县id
     * 查询他的下级的城市列表，也要给他加上
     * 
     * @param   $uid
     */
    public static function getCityNameById($uid){
        if(intval($uid)>0){
            $sql="select * from prf_operator_relation where (uid=:uid and useing=1) or (parent=:uid and useing=1)";
            $param=array(":uid"=>$uid);
            $operatorRealtionList=OperatorRelation::findBysql($sql,$param)->asArray()->all();
            $cityList_item=array();//城市列表
            $cityList_str='';
            for($i=0;$i<count($operatorRealtionList);$i++){
                $city_id=intval($operatorRealtionList[$i]['city_id']);
                $cityInfo=GrpCity::findBySql("select name from grp_city where id=:id",array(":id"=>$city_id))->asArray()->One();
                $city_name=$cityInfo['name'];
                $specilProvince=array('110000','120000','310000','500000');
                //city_id为省， 那么从city_list 获取cityid
                if($city_id%10000==0){
                    if(in_array($city_id, $specilProvince)){
                        //直辖市
                        $cityList_item[]="'".$city_name."'";
                    }else{
                        $citylistStr=$operatorRealtionList[$i]["city_list"];
                        //省份--根据city_list 获取城市 -- 可能多个城市
                        $citylist_array=explode(",",$citylistStr);
                        for($j=0;$j<count($citylist_array);$j++){
                            $cityid=$citylist_array[$j];
                            $prevId=substr($cityid, 0,2);
                            $provinceId=$prevId."0000";
                            $cityInfo=GrpCity::findBySql("select name from grp_city where id=:id",array(":id"=>$provinceId))->asArray()->One();
                            $provinceName=$cityInfo['name'];
                            $cityInfo=GrpCity::findBySql("select name from grp_city where id=:id",array(":id"=>$cityid))->asArray()->One();
                            $cityname=$cityInfo['name'];
                            $cityList_item[]="' ".$provinceName." ".$cityname."'";
                        }
                    }
                    //city_id为市
                }else{
                    if($city_id%100==0){//城市
                        $prevId=substr($city_id, 0,2);
                        $provinceId=$prevId."0000";
                        $cityInfo=GrpCity::findBySql("select name from grp_city where id=:id",array(":id"=>$provinceId))->asArray()->One();
                        $provinceName=$cityInfo['name'];
                        $cityList_item[]="'".$provinceName." ".$city_name."'";
                    }
                }
            }
            $cityList_item=array_unique($cityList_item);
            $cityList_str=implode(",",$cityList_item);
            return $cityList_str;
        }else{
            return "";
        }
    }
    /**
     * 获取当前的登录的运营商的城市名称    包括下级运营商   可能cityid为省份 直辖市
     * @return string
     */
    public static function getCityNameByIdNprovincename(){
        $uid=Yii::$app->user->identity->user_id;
        if(intval($uid)>0){
            $sql="select * from prf_operator_relation where (uid=:uid and useing=1) or (parent=:uid and useing=1)";
            $param=array(":uid"=>$uid);
            $operatorRealtionList=OperatorRelation::findBysql($sql,$param)->asArray()->all();
            $cityList_item=array();//城市列表
            $cityList_str='';
            for($i=0;$i<count($operatorRealtionList);$i++){
                $city_id=intval($operatorRealtionList[$i]['city_id']);
                $cityInfo=GrpCity::findBySql("select name from grp_city where id=:id",array(":id"=>$city_id))->asArray()->One();
                $city_name=$cityInfo['name'];
                $specilProvince=array('110000','120000','310000','500000');
                //city_id为省， 那么从city_list 获取cityid
                if($city_id%10000==0){
                    if(in_array($city_id, $specilProvince)){
                        //直辖市
                        $cityList_item[]="' ".$city_name."'";
                    }else{
                        $citylistStr=$operatorRealtionList[$i]["city_list"];
                        //省份--根据city_list 获取城市 -- 可能多个城市
                        $citylist_array=explode(",",$citylistStr);
                        for($j=0;$j<count($citylist_array);$j++){
                            $cityid=$citylist_array[$j];
                            $prevId=substr($cityid, 0,2);
                            $provinceId=$prevId."0000";
                            $cityInfo=GrpCity::findBySql("select name from grp_city where id=:id",array(":id"=>$provinceId))->asArray()->One();
                            $provinceName=$cityInfo['name'];
                            $cityInfo=GrpCity::findBySql("select name from grp_city where id=:id",array(":id"=>$cityid))->asArray()->One();
                            $cityname=$cityInfo['name'];
                            $cityList_item[]="' ".$cityname."'";
                        }
                    }
                    //city_id为市
                }else{
                    if($city_id%100==0){//城市
                        $prevId=substr($city_id, 0,2);
                        $provinceId=$prevId."0000";
                        $cityInfo=GrpCity::findBySql("select name from grp_city where id=:id",array(":id"=>$provinceId))->asArray()->One();
                        $provinceName=$cityInfo['name'];
                        $cityList_item[]="' ".$city_name."'";
                    }
                }
            }
            $cityList_item=array_unique($cityList_item);
            $cityList_str=implode(",",$cityList_item);
            return $cityList_str;
        }else{
            return "";
        }
    }

    /**
     * 获取某个运营商的城市名称    包括下级运营商   可能cityid为省份 直辖市
     * @return string
     */
    public static function getCityNameByOIdNprovincename($uid){
        if(intval($uid)>0){
            $sql="select * from prf_operator_relation where (uid=:uid and useing=1) or (parent=:uid and useing=1)";
            $param=array(":uid"=>$uid);
            $operatorRealtionList=OperatorRelation::findBysql($sql,$param)->asArray()->all();
            $cityList_item=array();//城市列表
            $cityList_str='';
            for($i=0;$i<count($operatorRealtionList);$i++){
                $city_id=intval($operatorRealtionList[$i]['city_id']);
                $cityInfo=GrpCity::findBySql("select name from grp_city where id=:id",array(":id"=>$city_id))->asArray()->One();
                $city_name=$cityInfo['name'];
                $specilProvince=array('110000','120000','310000','500000');
                //city_id为省， 那么从city_list 获取cityid
                if($city_id%10000==0){
                    if(in_array($city_id, $specilProvince)){
                        //直辖市
                        $cityList_item[]="' ".$city_name."'";
                    }else{
                        $citylistStr=$operatorRealtionList[$i]["city_list"];
                        //省份--根据city_list 获取城市 -- 可能多个城市
                        $citylist_array=explode(",",$citylistStr);
                        for($j=0;$j<count($citylist_array);$j++){
                            $cityid=$citylist_array[$j];
                            $prevId=substr($cityid, 0,2);
                            $provinceId=$prevId."0000";
                            $cityInfo=GrpCity::findBySql("select name from grp_city where id=:id",array(":id"=>$provinceId))->asArray()->One();
                            $provinceName=$cityInfo['name'];
                            $cityInfo=GrpCity::findBySql("select name from grp_city where id=:id",array(":id"=>$cityid))->asArray()->One();
                            $cityname=$cityInfo['name'];
                            $cityList_item[]="' ".$cityname."'";
                        }
                    }
                    //city_id为市
                }else{
                    if($city_id%100==0){//城市
                        $prevId=substr($city_id, 0,2);
                        $provinceId=$prevId."0000";
                        $cityInfo=GrpCity::findBySql("select name from grp_city where id=:id",array(":id"=>$provinceId))->asArray()->One();
                        $provinceName=$cityInfo['name'];
                        $cityList_item[]="' ".$city_name."'";
                    }
                }
            }
            $cityList_item=array_unique($cityList_item);
            $cityList_str=implode(",",$cityList_item);
            return $cityList_str;
        }else{
            return "";
        }
    }
   /**
   
     * 获取运营商城市名称  不包括下级
     * @return string
     */
    public static function getCityNameByIdNparent(){
        $uid=Yii::$app->user->identity->user_id;
        if(intval($uid)>0){
            $sql="select * from prf_operator_relation where (uid=:uid and useing=1) or (parent=:uid and useing=1)";
            $param=array(":uid"=>$uid);
            $operatorRealtionList=OperatorRelation::findBysql($sql,$param)->asArray()->all();
            $cityList_item=array();//城市列表
            $cityList_str='';
            for($i=0;$i<count($operatorRealtionList);$i++){
                $city_id=intval($operatorRealtionList[$i]['city_id']);
                $cityInfo=GrpCity::findBySql("select name from grp_city where id=:id",array(":id"=>$city_id))->asArray()->One();
                $city_name=$cityInfo['name'];
                $specilProvince=array('110000','120000','310000','500000');
                //city_id为省， 那么从city_list 获取cityid
                if($city_id%10000==0){
                    if(in_array($city_id, $specilProvince)){
                        //直辖市
                        $cityList_item[]="'".$city_name."'";
                    }else{
                        $citylistStr=$operatorRealtionList[$i]["city_list"];
                        //省份--根据city_list 获取城市 -- 可能多个城市
                        $citylist_array=explode(",",$citylistStr);
                        for($j=0;$j<count($citylist_array);$j++){
                            $cityid=$citylist_array[$j];
                            $prevId=substr($cityid, 0,2);
                            $provinceId=$prevId."0000";
                            $cityInfo=GrpCity::findBySql("select name from grp_city where id=:id",array(":id"=>$provinceId))->asArray()->One();
                            $provinceName=$cityInfo['name'];
                            $cityInfo=GrpCity::findBySql("select name from grp_city where id=:id",array(":id"=>$cityid))->asArray()->One();
                            $cityname=$cityInfo['name'];
                            $cityList_item[]=$cityname;
                        }
                    }
                    //city_id为市
                }else{
                    if($city_id%100==0){//城市
                        $prevId=substr($city_id, 0,2);
                        $provinceId=$prevId."0000";
                        $cityInfo=GrpCity::findBySql("select name from grp_city where id=:id",array(":id"=>$provinceId))->asArray()->One();
                        $cityList_item[]=$city_name;
                    }
                }
            }
            $cityList_item=array_unique($cityList_item);
            return $cityList_item;
        }else{
            return "";
        }
    }

    /**
     * 获取当前登录的管理员 /运营商的省份  可能单个 可能多个  
     * @param unknown $uid
     */
    public  static function getAllProvinceById(){
        $admin_id=Yii::$app->user->identity->id;
        $admin_info = AdminInfo::findOne(['id'=>$admin_id]);
        $uid=$admin_info['user_id'];
        if($admin_info['user_type'] == 1){//非平台管理员
            $province = GrpCity::findBySql("select * from grp_city where id like '%0000'")->asArray()->all();
        }else{//获取运营商的省份
            $sql="select * from prf_operator_relation where uid=:uid";
            $param=array(":uid"=>$uid);
            $operatorRealtionList=OperatorRelation::findBysql($sql,$param)->asArray()->all();
            $province=array();
            for($i=0;$i<count($operatorRealtionList);$i++){
                $city_id=intval($operatorRealtionList[$i]['city_id']);
                $province[]=substr($city_id,0,2)."0000";
            }
            $province=array_unique($province);  
            $provinceStr=implode(",",$province);
            $province = GrpCity::findBySql("select * from grp_city where id  in ({$provinceStr})")->asArray()->all();
        }
        return $province;
    }
   /**
    * 根据当前登录的运营商省份 获取城市
    */ 
    public static function getCitylistByIdAndProvince($province_id){
        $uid=Yii::$app->user->identity->user_id;
        $user_type=Yii::$app->user->identity->user_type;
        if(intval($user_type)>1){//运营商
            if(intval($uid)>0){
                $sql="select * from prf_operator_relation where uid=:uid";
                $param=array(":uid"=>$uid);
                $operatorRealtionList=OperatorRelation::findBysql($sql,$param)->asArray()->all();
                $cityList_item=array();//城市列表
                for($i=0;$i<count($operatorRealtionList);$i++){
                    $city_id=intval($operatorRealtionList[$i]['city_id']);
                    $specilProvince=array('110000','120000','310000','500000');
                    //city_id为省， 那么从city_list 获取cityid
                    if($city_id%10000==0){
                        if($city_id==intval($province_id)){
                            if(in_array($city_id, $specilProvince)){
                                $cityList_item[]=$city_id;
                            }else{
                                $citylistStr=$operatorRealtionList[$i]["city_list"];
                                //省份--根据city_list 获取城市 -- 可能多个城市
                                $citylist_array=explode(",",$citylistStr);
                                for($j=0;$j<count($citylist_array);$j++){
                                    $pronvince_prev=substr($province_id, 0,2);
                                    $city_prev=substr($city_id, 0,2);
                                    if($pronvince_prev==$city_prev){
                                        $cityList_item[]=$citylist_array[$j];
                                    }
                                    
                                }
                            }
                        }
                        //city_id为市
                    }else{
                        if($city_id%100==0){//城市
                            $pronvince_prev=substr($province_id, 0,2);
                            $city_prev=substr($city_id, 0,2);
                            if($pronvince_prev==$city_prev){
                                $cityList_item[]=$city_id;
                            }
                        }
                    }
                }
                $cityList_Str=implode(",",$cityList_item);
                if($cityList_Str!=""){
                    $cityList=GrpCity::findBySql("select * from grp_city where id in ({$cityList_Str})")->asArray()->all();
                }else{
                    $cityList=array();
                }
            }    
        }else{//所有城市
            $specilProvince=array('110000','120000','310000','500000');
            if(in_array($province_id, $specilProvince)){
                $cityList=GrpCity::findBySql("select * from grp_city where id=:id",array(":id"=>$province_id))->asArray()->all();
            }else{
                $cityList=GrpCity::city($province_id);
            }
        }
       return $cityList;
    }
   /**
    * 判断是不是存在香港 澳门 台湾
    */
    public static function getifXat(){
        $user_id=Yii::$app->user->identity->user_id;
        if(intval($user_id)>0){
            $ifxat=false;
            $sql="select * from prf_operator_relation where (uid=:uid or parent=:uid) and useing=1 ";
            $operatorList=OperatorRelation::findBySql($sql,array(":uid"=>$user_id))->asArray()->all();
            $specilProvince=array('710000','810000','820000');//香港 澳门  台湾
           // $specilProvince=array('110000','120000','310000','500000');//直辖市
            for($i=0;$i<count($operatorList);$i++){
                $city_list=explode(",",$operatorList[$i]['city_list']);
                $first_city=intval($city_list[0]);//110000 410000 330200
                if($first_city%10000==0){
                    if(in_array($first_city,$specilProvince)){ 
                        $ifxat=true;
                        break;
                    } 
                }
            }
            return $ifxat;
        }else{
            return false;
        }
    }
    /**
     * 判断某个运营商是不是港澳台 
     */
    public static function getifXatByOid($user_id){
        if(intval($user_id)>0){
            $ifxat=false;
            $sql="select * from prf_operator_relation where (uid=:uid or parent=:uid) and useing=1 ";
            $operatorList=OperatorRelation::findBySql($sql,array(":uid"=>$user_id))->asArray()->all();
            $specilProvince=array('710000','810000','820000');//香港 澳门  台湾
            // $specilProvince=array('110000','120000','310000','500000');//直辖市
            for($i=0;$i<count($operatorList);$i++){
                $city_list=explode(",",$operatorList[$i]['city_list']);
                $first_city=intval($city_list[0]);//110000 410000 330200
                if($first_city%10000==0){
                    if(in_array($first_city,$specilProvince)){
                        $ifxat=true;
                        break;
                    }
                }
            }
            return $ifxat;
        }else{
            return false;
        }
    }
    /**
     * 判断是不是存在直辖市
     */
    public static function getifZxs(){
        $user_id=Yii::$app->user->identity->user_id;
        if(intval($user_id)>0){
            $ifxat=false;
            $sql="select * from prf_operator_relation where (uid=:uid or parent=:uid) and useing=1 ";
            $operatorList=OperatorRelation::findBySql($sql,array(":uid"=>$user_id))->asArray()->all();
             $specilProvince=array('110000','120000','310000','500000');//直辖市
            for($i=0;$i<count($operatorList);$i++){
                $city_list=explode(",",$operatorList[$i]['city_list']);
                $first_city=intval($city_list[0]);//110000 410000 330200
                if($first_city%10000==0){
                    if(in_array($first_city,$specilProvince)){
                        $ifxat=true;
                        break;
                    }
                }
            }
            return $ifxat;
        }else{
            return false;
        }
    }
    /**
     * 判断当前运营商是否有市级别
     * @return boolean
     */
    public static function  ifhascity(){
        $user_id=Yii::$app->user->identity->user_id;
        $sql="select * from prf_operator_relation where (uid=:uid or parent=:uid) and useing=1 ";
        $operatorList=OperatorRelation::findBySql($sql,array(":uid"=>$user_id))->asArray()->all();
        $ifhascity=false;
        for($i=0;$i<count($operatorList);$i++){
            $citylist=explode(",", $operatorList[$i]['city_list']);
            $first_citylist=$citylist[0];
            if($first_citylist%100==0){//城市
                $ifhascity=true;
                break;
            } 
        }
        return $ifhascity;
    }
    /**
     * 判断某个运营商是否有市级别
     * @return boolean
     */
    public static function ifhascityByOid($user_id){
        $sql="select * from prf_operator_relation where (uid=:uid or parent=:uid) and useing=1 ";
        $operatorList=OperatorRelation::findBySql($sql,array(":uid"=>$user_id))->asArray()->all();
        $ifhascity=false;
        for($i=0;$i<count($operatorList);$i++){
            $citylist=explode(",", $operatorList[$i]['city_list']);
            $first_citylist=$citylist[0];
            if($first_citylist%100==0){//城市
                $ifhascity=true;
                break;
            }
        }
        return $ifhascity;
    }
    /**
     * 判断当前运营商是否有直辖市级别
     * @return boolean
     */
    public static function ifhaszxsCountry(){
        $user_id=Yii::$app->user->identity->user_id;
        $sql="select * from prf_operator_relation where (uid=:uid or parent=:uid) and useing=1 ";
        $operatorList=OperatorRelation::findBySql($sql,array(":uid"=>$user_id))->asArray()->all();
        $ifhaszxsCountry=false;
        for($i=0;$i<count($operatorList);$i++){
            $citylist=explode(",", $operatorList[$i]['city_list']);
            $first_citylist=$citylist[0];
            if($first_citylist%100!=0){ 
                $ifhascity=true;
                $prev=substr($first_citylist, 0,2);
                if(in_array($prev, array("11","12","31","50"))){//直辖市 区县
                    $ifhaszxsCountry=true;
                    break;
                } 
            } 
        }
        return $ifhaszxsCountry;
    }
    /**
     * 判断某个运营商是否有直辖市级别
     * @return boolean
     */
    public static function ifhaszxsCountryByOid($user_id){
        $sql="select * from prf_operator_relation where (uid=:uid or parent=:uid) and useing=1 ";
        $operatorList=OperatorRelation::findBySql($sql,array(":uid"=>$user_id))->asArray()->all();
        $ifhaszxsCountry=false;
        for($i=0;$i<count($operatorList);$i++){
            $citylist=explode(",", $operatorList[$i]['city_list']);
            $first_citylist=$citylist[0];
            if($first_citylist%100!=0){
                $ifhascity=true;
                $prev=substr($first_citylist, 0,2);
                if(in_array($prev, array("11","12","31","50"))){//直辖市 区县
                    $ifhaszxsCountry=true;
                    break;
                }
            }
        }
        return $ifhaszxsCountry;
    }
   /**
    * 判断当前运营商是否有区县级别
    * @return boolean
    */
    
    public static function ifhascountry(){
        $user_id=Yii::$app->user->identity->user_id;
        $sql="select * from prf_operator_relation where (uid=:uid or parent=:uid) and useing=1 ";
        $operatorList=OperatorRelation::findBySql($sql,array(":uid"=>$user_id))->asArray()->all();
        $ifhascountry=false;
        for($i=0;$i<count($operatorList);$i++){
            $citylist=explode(",", $operatorList[$i]['city_list']);
            $first_citylist=$citylist[0];
            if($first_citylist%100!=0){
                $ifhascity=true;
                $prev=substr($first_citylist, 0,2);
                if(!in_array($prev, array("11","12","31","50"))){//直辖市 区县
                    $ifhascountry=true;
                    break;
                } 
            }
        }
        return $ifhascountry;
    }
    /**
     * 判断某个运营商是否有区县级别
     * @return boolean
     */
    public static function ifhascountryByOid($user_id){
        $sql="select * from prf_operator_relation where (uid=:uid or parent=:uid) and useing=1 ";
        $operatorList=OperatorRelation::findBySql($sql,array(":uid"=>$user_id))->asArray()->all();
        $ifhascountry=false;
        for($i=0;$i<count($operatorList);$i++){
            $citylist=explode(",", $operatorList[$i]['city_list']);
            $first_citylist=$citylist[0];
            if($first_citylist%100!=0){
                $ifhascity=true;
                $prev=substr($first_citylist, 0,2);
                if(!in_array($prev, array("11","12","31","50"))){//直辖市 区县
                    $ifhascountry=true;
                    break;
                }
            }
        }
        return $ifhascountry;
    }
    /**
     * 判断当前运营商是不是省级
     */
    public static function ifhasprovince(){
        $user_id=Yii::$app->user->identity->user_id;
        $sql="select * from prf_operator_relation where (uid=:uid or parent=:uid) and useing=1 ";
        $operatorList=OperatorRelation::findBySql($sql,array(":uid"=>$user_id))->asArray()->all();
        $ifhasprovince=false;
        for($i=0;$i<count($operatorList);$i++){
            $citylist=explode(",", $operatorList[$i]['city_list']);
            $cityId=intval($operatorList[$i]['city_id']);
            if($cityId%10000==0){
                $specilProvince=array('110000','120000','310000','500000');//直辖市
                if(!in_array($cityId, $specilProvince)){
                    //判断city_list是不是当前省份全部市
                    $prev=substr($cityId, 0,2);
                    $citylist_count=count($citylist);
                    $sql="select count(id) c from grp_city where substr(id,1,2)=:prev and substr(id,5,6)='00'";
                    $grpcity=GrpCity::findBySql($sql,array(":prev"=>$prev))->asArray()->one();
                    $count=intval($grpcity['c']);
                    if($count==$citylist_count){
                        $ifhasprovince=true;
                        break;
                    }
                }
            }
        }  
        return $ifhasprovince;
    }
    /**
     * 判断某个运营商是不是省级
     */
    public static function ifhasprovinceByOid($user_id){
        $sql="select * from prf_operator_relation where (uid=:uid or parent=:uid) and useing=1 ";
        $operatorList=OperatorRelation::findBySql($sql,array(":uid"=>$user_id))->asArray()->all();
        $ifhasprovince=false;
        for($i=0;$i<count($operatorList);$i++){
            $citylist=explode(",", $operatorList[$i]['city_list']);
            $cityId=intval($operatorList[$i]['city_id']);
            if($cityId%10000==0){
                $specilProvince=array('110000','120000','310000','500000');//直辖市
                if(!in_array($cityId, $specilProvince)){
                    //判断city_list是不是当前省份全部市
                    $prev=substr($cityId, 0,2);
                    $citylist_count=count($citylist);
                    $sql="select count(id) c from grp_city where substr(id,1,2)=:prev and substr(id,5,6)='00'";
                    $grpcity=GrpCity::findBySql($sql,array(":prev"=>$prev))->asArray()->one();
                    $count=intval($grpcity['c']);
                    if($count==$citylist_count){
                        $ifhasprovince=true;
                        break;
                    }
                }
            }
        }
        return $ifhasprovince;
    }
   /**
    * 获取当前运营商所有省份  注：级别运营商
    */
    public static function getProvinceNameById(){
        $user_id=Yii::$app->user->identity->user_id;
        $sql="select * from prf_operator_relation where (uid=:uid or parent=:uid) and useing=1 ";
        $operatorList=OperatorRelation::findBySql($sql,array(":uid"=>$user_id))->asArray()->all();
        $provinceArray=array();
        $ProvinceNameArray=array();
        for($i=0;$i<count($operatorList);$i++){
            $citylist=explode(",", $operatorList[$i]['city_list']);
            $cityId=intval($operatorList[$i]['city_id']);
            if($cityId%10000==0){
                $specilProvince=array('110000','120000','310000','500000');//直辖市
                if(!in_array($cityId, $specilProvince)){
                    //判断city_list是不是当前省份全部市
                    $prev=substr($cityId, 0,2);
                    $citylist_count=count($citylist);
                    $sql="select count(id) c from grp_city where substr(id,1,2)=:prev and substr(id,5,6)='00'";
                    $grpcity=GrpCity::findBySql($sql,array(":prev"=>$prev))->asArray()->one();
                    $count=intval($grpcity['c']);
                    if($count==$citylist_count){
                        $ifhasprovince=true;
                        $provinceArray[]=$cityId;
                    }
                }
            }
        }  
        for($i=0;$i<count($provinceArray);$i++){
            $provinceId=$provinceArray[$i];
            $sql="select name from grp_city where id=:id";
            $param=array(":id"=>$provinceId);
            $provinceInfo=GrpCity::findBySql($sql,$param)->asArray()->one();
            $ProvinceNameArray[]="' ".$provinceInfo['name']."'";
        }
        if(count($ProvinceNameArray)>0){
            $provinceNameStr=implode(",",$ProvinceNameArray);
        }else{
            $provinceNameStr="";
        }
        return $provinceNameStr;
    }
    /**
     * 获取某个运营商所有省份  注：级别运营商
     */
    
    public  static function getProvinceNameByOId($user_id){
        $sql="select * from prf_operator_relation where (uid=:uid or parent=:uid) and useing=1 ";
        $operatorList=OperatorRelation::findBySql($sql,array(":uid"=>$user_id))->asArray()->all();
        $provinceArray=array();
        $ProvinceNameArray=array();
        for($i=0;$i<count($operatorList);$i++){
            $citylist=explode(",", $operatorList[$i]['city_list']);
            $cityId=intval($operatorList[$i]['city_id']);
            if($cityId%10000==0){
                $specilProvince=array('110000','120000','310000','500000');//直辖市
                if(!in_array($cityId, $specilProvince)){
                    //判断city_list是不是当前省份全部市
                    $prev=substr($cityId, 0,2);
                    $citylist_count=count($citylist);
                    $sql="select count(id) c from grp_city where substr(id,1,2)=:prev and substr(id,5,6)='00'";
                    $grpcity=GrpCity::findBySql($sql,array(":prev"=>$prev))->asArray()->one();
                    $count=intval($grpcity['c']);
                    if($count==$citylist_count){
                        $ifhasprovince=true;
                        $provinceArray[]=$cityId;
                    }
                }
            }
        }
        for($i=0;$i<count($provinceArray);$i++){
            $provinceId=$provinceArray[$i];
            $sql="select name from grp_city where id=:id";
            $param=array(":id"=>$provinceId);
            $provinceInfo=GrpCity::findBySql($sql,$param)->asArray()->one();
            $ProvinceNameArray[]="' ".$provinceInfo['name']."'";
        }
        if(count($ProvinceNameArray)>0){
            $provinceNameStr=implode(",",$ProvinceNameArray);
        }else{
            $provinceNameStr="";
        }
        return $provinceNameStr;
    }
    /**
     * 获取当前运营商 包括下级 运营商  所有区县
     */
    public static function getcountryNameById(){
        $user_id=Yii::$app->user->identity->user_id;
        $sql="select * from prf_operator_relation where (uid=:uid or parent=:uid) and useing=1 ";
        $operatorList=OperatorRelation::findBySql($sql,array(":uid"=>$user_id))->asArray()->all();
        $countryArray=array();
        $countryNameArray=array();
        for($i=0;$i<count($operatorList);$i++){
            $citylist=explode(",", $operatorList[$i]['city_list']);
            $cityId=intval($operatorList[$i]['city_id']);
            if($cityId%100==0){
                $citylist=explode(",", $operatorList[$i]['city_list']);
                $first_citylist=intval($citylist[0]);
                if($first_citylist%100!=0){//区县放进集合
                    for($z=0;$z<count($citylist);$z++){
                        $countryArray[]=$citylist[$z];
                    }
                }
            }
        }
        for($j=0;$j<count($countryArray);$j++){
            $countryId=$countryArray[$j];
            $provinceId=substr($countryId, 0,2)."0000";
            $specilProvince=array('110000','120000','310000','500000');
            //   如果是直辖市  XX市 xx区  
            if(in_array($provinceId, $specilProvince)){
                $sql="select name from grp_city where id=:id";
                $param=array(":id"=>$provinceId);
                $CityInfo=GrpCity::findBySql($sql,$param)->asArray()->one();
                $cityName=$CityInfo['name'];
            //   如果是普通省份  XX省 xx市 xx区
            }else{ 
                $sql="select name from grp_city where id=:id";
                $param=array(":id"=>$provinceId);
                $CityInfo=GrpCity::findBySql($sql,$param)->asArray()->one();
                $provinceName=$CityInfo['name'];
                $cityId=substr($countryId, 0,4)."00";
                $sql="select name from grp_city where id=:id";
                $param=array(":id"=>$cityId);
                $CityInfo=GrpCity::findBySql($sql,$param)->asArray()->one();
                $cityName=$CityInfo['name'];
            }
           
            $sql="select name from grp_city where id=:id";
            $param=array(":id"=>$countryId);
            $CountryInfo=GrpCity::findBySql($sql,$param)->asArray()->one();
            if(in_array($provinceId, $specilProvince)){
                $countryNameArray[]="'".$cityName." ".$CountryInfo['name']."'";
            }else{
                $countryNameArray[]="'".$provinceName." ".$cityName." ".$CountryInfo['name']."'";
            }
          //  $countryNameArray[]="' ".$CountryInfo['name']."'";
        }
        if(count($countryNameArray)>0){
            $countryNameStr=implode(",",$countryNameArray);
        }else{
            $countryNameStr="";
        }
        return $countryNameStr;
    }
    /**
     * 获取某个运营商 包括下级 运营商  所有区县
     */
    public static function getcountryNameByOId($user_id){
        $sql="select * from prf_operator_relation where (uid=:uid or parent=:uid) and useing=1 ";
        $operatorList=OperatorRelation::findBySql($sql,array(":uid"=>$user_id))->asArray()->all();
        $countryArray=array();
        $countryNameArray=array();
        for($i=0;$i<count($operatorList);$i++){
            $citylist=explode(",", $operatorList[$i]['city_list']);
            $cityId=intval($operatorList[$i]['city_id']);
            if($cityId%100==0){
                $citylist=explode(",", $operatorList[$i]['city_list']);
                $first_citylist=intval($citylist[0]);
                if($first_citylist%100!=0){//区县放进集合
                    for($z=0;$z<count($citylist);$z++){
                        $countryArray[]=$citylist[$z];
                    }
                }
            }
        }
        for($j=0;$j<count($countryArray);$j++){
            $countryId=$countryArray[$j];
            $provinceId=substr($countryId, 0,2)."0000";
            $specilProvince=array('110000','120000','310000','500000');
            //   如果是直辖市  XX市 xx区
            if(in_array($provinceId, $specilProvince)){
                $sql="select name from grp_city where id=:id";
                $param=array(":id"=>$provinceId);
                $CityInfo=GrpCity::findBySql($sql,$param)->asArray()->one();
                $cityName=$CityInfo['name'];
                //   如果是普通省份  XX省 xx市 xx区
            }else{
                $sql="select name from grp_city where id=:id";
                $param=array(":id"=>$provinceId);
                $CityInfo=GrpCity::findBySql($sql,$param)->asArray()->one();
                $provinceName=$CityInfo['name'];
                $cityId=substr($countryId, 0,4)."00";
                $sql="select name from grp_city where id=:id";
                $param=array(":id"=>$cityId);
                $CityInfo=GrpCity::findBySql($sql,$param)->asArray()->one();
                $cityName=$CityInfo['name'];
            }
            $sql="select name from grp_city where id=:id";
            $param=array(":id"=>$countryId);
            $CountryInfo=GrpCity::findBySql($sql,$param)->asArray()->one();
            if(in_array($provinceId, $specilProvince)){
                $countryNameArray[]="'".$cityName." ".$CountryInfo['name']."'";
            }else{
                $countryNameArray[]="'".$provinceName." ".$cityName." ".$CountryInfo['name']."'";
            }
        }
        if(count($countryNameArray)>0){
            $countryNameStr=implode(",",$countryNameArray);
        }else{
            $countryNameStr="";
        }
        return $countryNameStr;
    }
   
    /**
     * 获取当前运营商  所有区县
     * @return string
     */
    public static function getcountryNameByIdNparent(){
        $user_id=Yii::$app->user->identity->user_id;
        $sql="select * from prf_operator_relation where  uid=:uid  and useing=1 ";
        $operatorList=OperatorRelation::findBySql($sql,array(":uid"=>$user_id))->asArray()->all();
        $countryArray=array();
        $countryNameArray=array();
        for($i=0;$i<count($operatorList);$i++){
            $citylist=explode(",", $operatorList[$i]['city_list']);
            $cityId=intval($operatorList[$i]['city_id']);
            if($cityId%100==0){
                $citylist=explode(",", $operatorList[$i]['city_list']);
                $first_citylist=intval($citylist[0]);
                if($first_citylist%100!=0){//区县放进集合
                    for($z=0;$z<count($citylist);$z++){
                        $countryArray[]=$citylist[$z];
                    }
                }
            }
        }
        for($j=0;$j<count($countryArray);$j++){
            $countryId=$countryArray[$j];
            $provinceId=substr($countryId, 0,2)."0000";
            $specilProvince=array('110000','120000','310000','500000');
            //   如果是直辖市  XX市 xx区
            if(in_array($provinceId, $specilProvince)){
                $sql="select name from grp_city where id=:id";
                $param=array(":id"=>$provinceId);
                $CityInfo=GrpCity::findBySql($sql,$param)->asArray()->one();
                $cityName=$CityInfo['name'];
                //   如果是普通省份  XX省 xx市 xx区
            }else{
                $sql="select name from grp_city where id=:id";
                $param=array(":id"=>$provinceId);
                $CityInfo=GrpCity::findBySql($sql,$param)->asArray()->one();
                $provinceName=$CityInfo['name'];
                $cityId=substr($countryId, 0,4)."00";
                $sql="select name from grp_city where id=:id";
                $param=array(":id"=>$cityId);
                $CityInfo=GrpCity::findBySql($sql,$param)->asArray()->one();
                $cityName=$CityInfo['name'];
            }
            
            $sql="select name from grp_city where id=:id";
            $param=array(":id"=>$countryId);
            $CountryInfo=GrpCity::findBySql($sql,$param)->asArray()->one();
            if(in_array($provinceId, $specilProvince)){
                $countryNameArray[]="'".$cityName." ".$CountryInfo['name']."'";
            }else{
                $countryNameArray[]="'".$provinceName." ".$cityName." ".$CountryInfo['name']."'";
            }
        }
     
        return $countryNameArray;
    }
    /**
     * 根据区县获取城市名称
     */
    public static function getCityNameByCountry(){
        $user_id=Yii::$app->user->identity->user_id;
        $sql="select * from prf_operator_relation where (uid=:uid or parent=:uid) and useing=1 ";
        $operatorList=OperatorRelation::findBySql($sql,array(":uid"=>$user_id))->asArray()->all();
        $cityList_item=array();
        for($i=0;$i<count($operatorList);$i++){
            $city_list_Str=$operatorList[$i]['city_list'];
            $city_list=explode(",",$city_list_Str);
            $countryId=$city_list[0];
            $cityId=substr($countryId, 0,4)."00";
            $cityInfo=GrpCity::findBySql("select name from grp_city where id=:id",array(":id"=>$cityId))->asArray()->One();
            $cityname= "' ".$cityInfo['name']."'";
            $cityList_item[]=$cityname;
        }
        $cityList_item=array_unique($cityList_item);
        $cityLIst_str=implode(",", $cityList_item);
        return  $cityLIst_str;
    }
    
}
